<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" ghost @click="handleExportExcel()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'mold:repair:asdMoldUpdate:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleViewForm({ id: record.id })">
          {{ record.id }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <InputFormView @register="registerDrawerView" @success="handleSuccess" />
    <FalForm @register="registerFalDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
    <component
      :is="inputFormComponent"
      v-model:visible="inputFormVisible"
      @register="registerDrawerBpmMyTask"
      @success="handleSuccess"
    />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoldRepairAsdMoldUpdateList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, shallowRef } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { dynamicImport } from '/@/router/helper/routeHelper';
  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import qs from 'qs';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { asdMoldUpdateDelete, asdMoldUpdateListData } from '/@/api/mold/repair/asdMoldUpdate';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import InputFormView from './formView.vue';
  import FalForm from '/@/views/mold/fal/asdMoldFal/form.vue';
  import { bpmTaskDefForm } from '/@/api/bpm/myTask';
  import { basMoldClsTreeData } from '/@/api/bas/mold/basMoldCls';
  import globalSetting from '/@/enums/globalSetting';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';

  const { t } = useI18n('mold.repair.asdMoldUpdate');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('维修记录管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工装代码'),
        field: 'basMold.caddcode',
        component: 'Input',
      },
      {
        label: t('工装编码'),
        field: 'moldCode',
        component: 'Input',
      },
      {
        label: t('工装名称'),
        field: 'basMold.name',
        component: 'Input',
      },
      {
        label: t('工装类型'),
        field: 'basMold.basMoldCls.code',
        fieldLabel: 'basMold.basMoldCls.name',
        component: 'TreeSelect',
        componentProps: {
          api: basMoldClsTreeData,
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('工装尾号'),
        field: 'lastNum',
        component: 'Input',
      },
      {
        label: t('工序'),
        field: 'operCode',
        component: 'Input',
      },
      {
        label: t('存货代码'),
        field: 'basInv.cinvaddcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'basInv.cinvcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('登记日期'),
        field: 'ddate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('设备名称'),
        field: 'devName',
        component: 'Input',
      },
      {
        label: t('维修日期'),
        field: 'repairDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [
      ['ddate', ['ddate_gte', 'ddate_lte']],
      ['repairDate', ['repairDate_gte', 'repairDate_lte']],
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('维修记录ID'),
      dataIndex: 'id',
      key: 'a.id',
      sorter: true,
      width: 130,
      align: 'left',
      fixed: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('工装代码'),
      dataIndex: 'basMold.caddcode',
      key: 'mold.caddcode',
      sorter: true,
      width: 130,
      align: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('工装编码'),
      dataIndex: 'moldCode',
      key: 'a.mold_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工装尾号'),
      dataIndex: 'lastNum',
      key: 'lastNum',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工装名称'),
      dataIndex: 'basMold.name',
      key: 'mold.name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('工装类型'),
      dataIndex: 'basMold.basMoldCls.name',
      key: 'mold.cls_id',
      width: 80,
      align: 'left',
    },

    {
      title: t('工序名称'),
      dataIndex: 'basMold.operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'a.oper_code',
      // sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'basInv.cinvcode',
      key: 'a.oper_code',
      // sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'a.oper_code',
      // sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('登记日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('设备名称'),
      dataIndex: 'devName',
      key: 'a.dev_name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('维修日期'),
      dataIndex: 'repairDate',
      key: 'a.repair_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('维修描述'),
      dataIndex: 'repairDescription',
      key: 'a.repair_description',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('维修时长(小时)'),
      dataIndex: 'repairTime',
      key: 'a.repairTime',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('维修者'),
      dataIndex: 'empUser.userName',
      // key: 'a.repairName',
      // sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'left',
    },
    {
      title: t('修改日期'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('检验结果'),
      dataIndex: 'checkResult',
      key: 'a.checkResult',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'repair_and_keep_status',
      fixed: 'right',
    },
    {
      title: t('流程节点'),
      dataIndex: 'bpmTask.name',
      key: 'bpm.name',
      // sorter: true,
      width: 100,
      fixed: 'right',
    },
    {
      title: t('工装变更'),
      dataIndex: 'moldType',
      key: 'a.moldType',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'moldType',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑维修记录'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'mold:repair:asdMoldUpdate:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除维修记录'),
        popConfirm: {
          title: t('是否确认删除维修记录'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'mold:repair:asdMoldUpdate:edit',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
      // {
      //   label: t('首件确认'),
      //   title: t('首件确认'),
      //   onClick: handleFalForm.bind(this, record),
      //   auth: 'mold:repair:asdMoldUpdate:edit',
      // },
      {
        icon: 'mdi:checkbox-marked-outline',
        title: t('任务办理'),
        onClick: handleBpmMyTask.bind(this, { id: record.id }),
        ifShow: () => record.status == '4' || record.status == '5' || record.status == '6',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawerView, { openDrawer: openViewDrawer }] = useDrawer();
  const [registerFalDrawer, { openDrawer: openFalDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: asdMoldUpdateListData,
    beforeFetch: (params) => {
      console.log(params);
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  function handleViewForm(record: Recordable) {
    openViewDrawer(true, record);
  }

  // 首件确认记录
  function _handleFalForm(record: Recordable) {
    openFalDrawer(true, {
      hid: record.id,
      moldCode: record.moldCode,
      lastNum: record.lastNum,
      operCode: record.operCode,
      cinvcode: record.basInv.cinvcode,
      cinvname: record.basInv.cinvname,
    });
  }

  async function handleDelete(record: Recordable) {
    const res = await asdMoldUpdateDelete(record);
    showMessage((res as any).message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    const formKey = ref<string>('ASD_MOLD_REPAIR');
    console.log(record);
    if (record.basMold.clsCode == globalSetting.traceStatusEnum.traceStatus) {
      formKey.value = 'ASD_MOLD_REPAIR2';
    } else {
      formKey.value = 'ASD_MOLD_REPAIR';
    }
    traceModel(true, { formKey: formKey.value, bizKey: record.id });
  }

  const [registerDrawerBpmMyTask, { setDrawerData }] = useDrawer();
  const inputFormComponent = shallowRef<Nullable<any>>(null);
  const inputFormVisible = ref<Boolean>(false);
  async function handleBpmMyTask(record: Recordable) {
    const data = await bpmTaskDefForm({ formKey: 'ASD_MOLD_REPAIR', bizKey: record.id });
    if (data.result == 'true') {
      const url = data.pcUrl;
      const idx = url.indexOf('?');
      // component
      let component: ReturnType<typeof defineComponent>;
      const compStr = idx == -1 ? url : url.substring(0, idx);
      if (compStr && compStr != '') {
        const imp = dynamicImport(compStr);
        if (imp) component = createAsyncComponent(imp);
      }
      // params
      let params = {};
      const paramStr = idx == -1 ? '' : url.substring(idx + 1);
      if (paramStr && paramStr != '') {
        params = qs.parse(paramStr);
      }
      // open
      if (component) {
        inputFormComponent.value = component;
        inputFormVisible.value = true;
        setDrawerData(params);
      }
    } else {
      showMessage(data.message);
    }
  }

  function handleExportExcel() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/mold/repair/asdMoldUpdate/exportData',
      params: getForm().getFieldsValue(),
    });
  }
</script>
